package com.jiajia.study.ware.service.impl;

import com.jiajia.study.ware.constant.WareConstant;
import com.jiajia.study.ware.entity.PurchaseDetailEntity;
import com.jiajia.study.ware.service.PurchaseDetailService;
import com.jiajia.study.ware.service.WareSkuService;
import com.jiajia.study.ware.vo.MergeVO;
import com.jiajia.study.ware.vo.PurchaseDoneVo;
import com.jiajia.study.ware.vo.PurchaseItemDoneVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiajia.common.utils.PageUtils;
import com.jiajia.common.utils.Query;

import com.jiajia.study.ware.dao.PurchaseDao;
import com.jiajia.study.ware.entity.PurchaseEntity;
import com.jiajia.study.ware.service.PurchaseService;
import org.springframework.transaction.annotation.Transactional;


@Service("purchaseService")
public class PurchaseServiceImpl extends ServiceImpl<PurchaseDao, PurchaseEntity> implements PurchaseService {

    @Autowired
    private PurchaseDetailService purchaseDetailService;

    @Autowired
    private WareSkuService wareSkuService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),
                new QueryWrapper<PurchaseEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional
    public void merge(MergeVO mergeVO) {


        Long purchaseId = mergeVO.getPurchaseId();
        if(null == purchaseId){
            PurchaseEntity purchaseEntity = new PurchaseEntity();

            purchaseEntity.setStatus(0);
            purchaseEntity.setCreateTime(new Date());
            purchaseEntity.setUpdateTime(new Date());
            this.save(purchaseEntity);
            purchaseId = purchaseEntity.getId();
        }

        // todo : 确认 status : 0/1 才可以合并
        List<Long> items = mergeVO.getItems();

        Long finalPurchaseId = purchaseId;
        List<PurchaseDetailEntity> purchaseDetailEntities = items.stream().map(id -> {
            PurchaseDetailEntity purchaseDetailEntity = new PurchaseDetailEntity();
            PurchaseEntity purchaseEntity = this.getById(id);
            if (null == purchaseEntity) {
                return null;
            }
            purchaseDetailEntity.setId(id);
            purchaseDetailEntity.setStatus(WareConstant.PurchaseDetailStatusEnum.ASSIGNED.getCode());
            // 设置采购单id
            purchaseDetailEntity.setPurchaseId(finalPurchaseId);
            return purchaseDetailEntity;
        }).filter(Objects::nonNull).collect(Collectors.toList());

        purchaseDetailService.updateBatchById(purchaseDetailEntities);

        // 合并完成，修改更新时间
        PurchaseEntity purchaseEntity = new PurchaseEntity();
        purchaseEntity.setId(purchaseId);
        purchaseEntity.setUpdateTime(new Date());
        this.updateById(purchaseEntity);
    }

    @Override
    @Transactional
    public void received(List<Long> ids) {
        Collection<PurchaseEntity> purchaseEntities = this.listByIds(ids);

        List<PurchaseEntity> purchaseEntityList = purchaseEntities.stream()
                .filter(item -> item.getStatus() == WareConstant.PurchaseStatusEnum.CREATED.getCode() || item.getStatus() == WareConstant.PurchaseStatusEnum.ASSIGNED.getCode())
                .map(item -> {
                    item.setStatus(WareConstant.PurchaseStatusEnum.RECEIVE.getCode());
                    item.setUpdateTime(new Date());
                    return item;
                }).collect(Collectors.toList());

        this.updateBatchById(purchaseEntityList);

        // 改变采购项的状态
        purchaseDetailService.updateBatchStatus(purchaseEntityList.stream().map(PurchaseEntity::getId).collect(Collectors.toList()));
    }

    @Override
    public void finish(PurchaseDoneVo doneVo) {
        Long purchaseId = doneVo.getId();
        // 采购完成的状态，所有的采购条目都完成
        Boolean flag = true;

        List<PurchaseItemDoneVo> items = doneVo.getItems();
        List<PurchaseDetailEntity> updateDetailList = new ArrayList<>();
        for (PurchaseItemDoneVo item : items) {
            PurchaseDetailEntity purchaseDetailEntity = new PurchaseDetailEntity();

            if(item.getStatus() == WareConstant.PurchaseDetailStatusEnum.HASERROR.getCode()){
                flag = false;
                purchaseDetailEntity.setStatus(WareConstant.PurchaseDetailStatusEnum.HASERROR.getCode());
            }else {
                PurchaseDetailEntity detailEntity = purchaseDetailService.getById(item.getItemId());
                purchaseDetailEntity.setStatus(WareConstant.PurchaseDetailStatusEnum.FINISH.getCode());
                wareSkuService.addStock(detailEntity.getSkuId(),detailEntity.getWareId(),detailEntity.getSkuNum());
            }
            purchaseDetailEntity.setId(item.getItemId());
            updateDetailList.add(purchaseDetailEntity);
        }

        purchaseDetailService.updateBatchById(updateDetailList);

        // 改变订单的状态
        PurchaseEntity purchaseEntity = new PurchaseEntity();
        purchaseEntity.setStatus(flag ? WareConstant.PurchaseStatusEnum.FINISH.getCode() : WareConstant.PurchaseStatusEnum.HASERROR.getCode());
        purchaseEntity.setId(purchaseId);
        purchaseEntity.setUpdateTime(new Date());
        this.updateById(purchaseEntity);
    }

}